//
//  OrderManager.m
//  TCProject
//
//  Created by admin on 16/1/20.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "OrderManager.h"
#import "AccountManager.h"
@interface OrderManager ()

@property (nonatomic, strong) BMPage *page1;    //进行中订单
@property (nonatomic, strong) BMPage *page2;    //已完成订单
@property (nonatomic, strong) BMPage *page3;    //全部订单

@end
@implementation OrderManager


- (instancetype)init
{
    if (self = [super init]) {
        _page1 = [BMPage defaultPage];
        _page2 = [BMPage defaultPage];
        _page3 = [BMPage defaultPage];
    }
    return self;
}

- (void)getDispatcherOrderDetail:(ZDOrder *)order complete:(completeListener)listener
{
    BMRequest *request = [BMRequest requestWithPath:@"getDispatcherOrderDetail.htm" contentKey:@"disOrderDetail"];
    request.params = @{@"orderId": @(order.ID)};
    [ZDOrder postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)getOwnerOrderDetail:(CZOrder *)order own:(BOOL)isOwn complete:(completeListener)listener
{
    BMRequest *request = [BMRequest requestWithPath:@"getOrderById.htm" contentKey:@"orderInfo"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = @(order.ID);
    if (isOwn) {
        params[@"ownerId"] = @([AccountManager sharedInstance].user.ID);
        NSString *status = order.statusId == ZDOwnerStatusCommented ? @"已评价" : @"我的抢单";
        params[@"status"] = status;
    } else {
        params[@"ownerId"] = @([AccountManager sharedInstance].user.ID);
        params[@"status"] = @"";
    }
    request.params = params;
    [CZOrder postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)getOrderList:(NSInteger)type complete:(completeListener)listener
{
    NSMutableDictionary *params;
    switch (type) {
        case 1:
            params = _page1.nextPage;
            break;
        case 2:
            params = _page2.nextPage;
            break;
        case 3:
            params = _page3.nextPage;
            break;
    }
    if (!params) {
        listener(nil, [NSError errorWithDomain:kBMErrorDomain code:-1 userInfo:@{@"msg": @"已经到了最后一页，没有更多数据了。"}]);
        return;
    }
    params[@"type"] = @(type);
    if ([[AccountManager sharedInstance] isLogin]) {
        params[@"dispatcherId"] = @([AccountManager sharedInstance].user.ID);
    }
    if ([[AccountManager sharedInstance] isLogin]) {
        params[@"ownerId"] = @([AccountManager sharedInstance].user.ID);
    }
    BMRequest *request = [BMRequest requestWithPath:@"getDispatcherOrderList.htm" contentKey:@"DispatcherOrderVOList"];
    request.params = params;
    [ZDOrder postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        switch (type) {
            case 1:
                _page1 = response.page;
                break;
            case 2:
                _page2 = response.page;
                break;
            case 3:
                _page3 = response.page;
                break;
        }
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)getOrders:(NSInteger)type complete:(completeListener)listener
{
    NSMutableDictionary *params;
    switch (type) {
        case 1:
            params = _page1.nextPage;
            break;
        case 2:
            params = _page2.nextPage;
            break;
        case 3:
            params = _page3.nextPage;
            break;
    }
    if (!params) {
        listener(nil, [NSError errorWithDomain:kBMErrorDomain code:-1 userInfo:@{@"msg": @"已经到了最后一页，没有更多数据了。"}]);
        return;
    }
    params[@"type"] = @(type);
    
    if ([[AccountManager sharedInstance] isLogin]) {
        params[@"ownerId"] = @([AccountManager sharedInstance].user.ID);
    }
    
    //    CLLocationCoordinate2D location = [BMAccountManager sharedInstance].location;
    //    if (location.longitude !=0 && location.latitude != 0) {
    //        params[@"longitude"] = @(location.longitude);
    //        params[@"latitude"] = @(location.latitude);
    //    }
    
    BMRequest *request = [BMRequest requestWithPath:@"getOrders.htm" contentKey:@"orders"];
    request.params = params;
    [CZOrder postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        switch (type) {
            case 1:
                _page1 = response.page;
                break;
            case 2:
                _page2 = response.page;
                break;
            case 3:
                _page3 = response.page;
                break;
        }
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)getOwnerOrderList:(NSInteger)type complete:(completeListener)listener
{
    NSMutableDictionary *params;
    switch (type) {
        case 1:
            params = _page1.nextPage;
            break;
        case 2:
            params = _page2.nextPage;
            break;
        case 3:
            params = _page3.nextPage;
            break;
    }
    if (!params) {
        listener(nil, [NSError errorWithDomain:kBMErrorDomain code:-1 userInfo:@{@"msg": @"已经到了最后一页，没有更多数据了。"}]);
        return;
    }
    params[@"type"] = @(type);
    
    if ([[AccountManager sharedInstance] isLogin]) {
        params[@"ownerId"] = @([AccountManager sharedInstance].user.ID);
    }
    BMRequest *request = [BMRequest requestWithPath:@"getMyOrders.htm" contentKey:@"myOrders"];
    request.params = params;
    [CZOrder postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        switch (type) {
            case 1:
                _page1 = response.page;
                break;
            case 2:
                _page2 = response.page;
                break;
            case 3:
                _page3 = response.page;
                break;
        }
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)resetWithType:(NSInteger)type
{
    switch (type) {
        case 1:
            _page1 = [BMPage defaultPage];
            break;
        case 2:
            _page2 = [BMPage defaultPage];
            break;
        case 3:
            _page3 = [BMPage defaultPage];
            break;
    }
}

- (void)createOrder:(ZDOrder *)order complete:(completeListener)listener
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if ([[AccountManager sharedInstance] isLogin]) {
        params[@"dispatcherId"] = @([AccountManager sharedInstance].user.ID);
    }
    if (order.style.name) {
        params[@"style1"] = order.style.name;
    }
    if (order.style.ID) {
        params[@"priceId"] = @(order.style.ID);
    }
    if (order.style.condition) {
        params[@"condition"] =order.style.condition;
    }
    if (order.style.conditionName) {
        params[@"conditionName"] =order.style.conditionName;
    }
    if (order.volume) {
        params[@"volume"] = @(order.volume);
    }
    if (order.count) {
        params[@"count"] = @(order.count);
    }
    if (order.style.price) {
        params[@"unitPrice"] = order.style.price;
    }
    if (order.changeUnitPrice) {
        params[@"changeUnitPrice"] = order.changeUnitPrice;
    }
    if (order.name) {
        params[@"appointmentTime"] = order.name;
    }
    if (order.remarks) {
        params[@"remarks"] = order.remarks;
    }
    if (order.sumPrice) {
        params[@"sumPrice"] = @(order.sumPrice);
    }
    
    BMRequest *request = [BMRequest requestWithPath:@"createOrder.htm"];
    request.params = params;
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)cancleOrderByDispatcher:(ZDOrder *)order complete:(completeListener)listener
{
    BMRequest *request = [BMRequest requestWithPath:@"cancleOrderByDispatcher.htm"];
    request.params = @{@"dispatcherId":@([AccountManager sharedInstance].user.ID), @"orderId": @(order.ID), @"orderNum": order.orderNum, @"orderSumPrice": @(order.sumPrice)};
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        if (listener) {
            listener(response, error);
        }
    }];
}


@end
